// 封装jquery.form.min.js和layer.js-----常用方法
//1.0 

//----JS 公共FORM提交 by 蓝俊杰
var loading_layer;

function submit_action(objForm, callback_request, callback_response, dataType, method, timeout) {
    var options = {
        target: '#output_test', //把服务器返回的内容放入id为output_test的元素中作为测试
        beforeSubmit: function(formData, jqForm, options) {
            var formElement = jqForm[0];
            var message = new Array();
            for (var i = 0; i < formElement.length; i++) {
                if ($(formElement[i]).attr("validate")) {
                    validate = $(formElement[i]).attr("validate");
                    value = $(formElement[i]).attr("value");
                    if (!$.isEmptyObject(value)) {
                        validate_arr = validate.split(';');
                        for (var j = 0; j < validate_arr.length; j++) {
                            switch (validate_arr[j]) {
                                case 'email':
                                    if (!isEmail(value)) {
                                        message.push('邮箱地址格式不正确！');
                                    }
                                    break;
                                case 'mobile':
                                    if (!isMobile(value)) {
                                        message.push('手机格式不正确！');
                                    }
                                    break;
                                case 'numeric':
                                    if (!isNumeric(value)) {
                                        message.push('不是的数字型！');
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
            if (message.length > 0) {
                show_error(message.join("<br>"));
                return false;
            }
            if (isfunc(callback_request)) {
                if (callback_request(formData, jqForm, options)) {
                    loading_layer = layer.load(0, { shade: false });
                    return true;
                }
            }
            loading_layer = layer.load(0, { shade: false });
            return true;
        }, //提交前的回调函数
        success: function(data, statusText, xhr, $form) {
            setTimeout(function() {
                layer.closeAll('loading');
                if (isfunc(callback_response)) {
                    callback_response(data, statusText, xhr, $form);
                }
            }, 400);
        }, //成功提交后回调函数
        //url: url,                 //默认是form的action， 如果申明，则会覆盖
        type: method || "get", //默认是form的method（get or post），如果申明，则会覆盖
        dataType: dataType || "json", //html(默认), xml, script, json...接受服务端返回的类型
        clearForm: false, //成功提交后，清除所有表单元素的值
        resetForm: false, //成功提交后，重置所有表单元素的值
        timeout: timeout || 3000 //限制请求的时间，当请求大于3秒后，跳出请求
    }
    $("#" + objForm).submit(function() {
        $(this).ajaxSubmit(options);
        return false;
    });
}



//JS pc layer 失败 by 蓝俊杰
function show_error(msg, showicon) {
    showicon = showicon || 5;
    layer.msg(msg, { icon: showicon });
}
//JS pc layer 成功 by  蓝俊杰
function show_success(msg, showicon) {
    showicon = showicon || 1;
    layer.msg(msg, { icon: 1 });
}
//js pc Layer 打开一个loading by 蓝俊杰
function open_loading(type) {
    return layer.load(type);
}
//js pc Layer 关闭一个loading by 蓝俊杰
function close_loading(index) {
    layer.close(index);
}


//JS Tab选择器 by 蓝俊杰
function set_onMenuselect(objclass, selectedclass) {
    $("." + objclass).each(function(index, element) {
        $(this).mouseover(function() {
            var sel_index = $(this).attr("real");
            $("." + objclass).each(function(index, element) {
                curr_index = $(this).attr("real");
                if (curr_index != sel_index) {
                    $(this).removeClass(selectedclass);
                    $("#" + objclass + "_" + curr_index).hide();
                }
            });
            $(this).addClass(selectedclass);
            $("#" + objclass + "_" + sel_index).show();
        });
        $(this).attr("real", index);
    });
}

//滚动加载页面模块  by 蓝俊杰
//
function scroll_module_load(obj_class, callback) {
    $(window).bind("scroll", function(event) {
        $("." + obj_class + "[data-lock!=true]").each(function(index, element) {
            var thisButtomTop = parseInt($(window).height()) + parseInt($(window).scrollTop());
            var thisTop = parseInt($(window).scrollTop()); //屏幕顶部距离最顶部的高度
            var PictureTop = parseInt($(this).offset().top);
            var dataType = $(this).attr("data-type") || "html";
            if (PictureTop >= thisTop && PictureTop <= thisButtomTop) {
                data_url = $(this).attr("data-url");
                curr_obj = $(this);
                curr_obj.attr("data-lock", true);
                if (data_url) {
                    $.ajax({
                        url: data_url,
                        cache: false,
                        dataType: dataType || "html",
                        success: function(data) {
                            if (dataType == 'html') {
                                curr_obj.empty();
                                curr_obj.append(data);
                            }

                            if (isfunc(callback)) { callback($("." + obj_class), data); }
                        }
                    });
                }
            }
        });
    });
}

//验证方法   by 蓝俊杰
function isEmail(str) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    return reg.test(str);
}

function isNumeric(str) {
    return $.isNumeric(str);
}

function isMobile(str) {
    var isMobile = /^(?:13\d|15\d)\d{5}(\d{3}|\*{3})$/;
    return isMobile.test(str);
}

//是否方法   by 蓝俊杰
function isfunc(obj) {
    if (obj) {
        if (typeof obj === 'function') {
            return true;
        }
    }
    return false;
}
//当前浏览器是否android   by 蓝俊杰
function isAndroid() {
    var u = navigator.userAgent;
    if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) {
        return true;
    } else {
        return false;
    }
}
//当前浏览器是否WeiXin   by 蓝俊杰
function isWeiXin() {
    var u = window.navigator.userAgent.toLowerCase();
    if (u.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}
//当前浏览器是否Ios   by 蓝俊杰
function isIos() {
    var u = navigator.userAgent;
    if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
        return true;
    } else {
        return false;
    }
}
//检查手机浏是否安装了APP 是，启动APP，否，定向到下载页面  by 蓝俊杰
function runApp(app_url_schemes, download_url) {
    if (isAndroid()) { //android
        var state = null;
        try { state = window.open(app_url_schemes, '_blank'); } catch (e) {}
        if (state) {
            window.close();
        } else { window.location = download_url; }
    } else if (isIos()) { //ios
        var loadDateTime = new Date();
        window.setTimeout(function() {
            var timeOutDateTime = new Date();
            if (timeOutDateTime - loadDateTime < 5000) {
                window.location = download_url;
            } else { window.close(); }
        }, 25);
        window.location = app_url_schemes;
    } else if (isWeiXin()) {} //weixin
}
//读URL参数  by 蓝俊杰
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null)
        context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
}